<?php

namespace app\index\service;

use app\common\job\ConsultingCommentStatistics;
use app\common\model\Consulting;
use app\common\model\ConsultingComment;
use app\common\model\ConsultingMessage;
use app\common\model\Trade;
use app\index\dao\ConsultingCommentDao;
use app\index\model\UM as UM;
use think\exception\ValidateException;
use think\facade\Queue;

/**
 * Class CardMessageCommentService
 * @package app\consulting\service
 */
class MessageCardCommentService
{
    // dao
    private ConsultingCommentDao $consultingCommentDao;

    public function __construct(ConsultingCommentDao $consultingCommentDao)
    {
        $this->consultingCommentDao = $consultingCommentDao;
    }

    /**
     * 新增
     * @param \app\index\model\UM $user
     * @param array $params
     */
    public function add(UM $user, array $params): void
    {
        $memberId = $user->id;
        $consultingMessageId = $params['consultingMessageId'] ?? 0;
        $tradeId = $params['tradeId'] ?? 0;
        $params['level'] ??= null;
        $params['comment'] ??= '';

        if (!is_id($consultingMessageId) && !is_id($tradeId)) {
            throw new ValidateException('请选择一个消息或订单');
        }
        if (!is_numeric($params['level'])) {
            throw new ValidateException('请选择评价星级');
        }
        $params['level'] = (int)$params['level'];
        if ($params['level'] < 0) {
            throw new ValidateException('至少要选一个星星');
        }
        if (is_blank($params['comment'])) {
            throw new ValidateException('请输入评价内容');
        }

        if (is_id($consultingMessageId)) {

            $this->commentMessage($memberId, $params);
        } elseif (is_id($tradeId)) {

            $this->commentTrade($memberId, $params);
        }
    }

    private function commentTrade($memberId, $params)
    {
        $tradeId = $params['tradeId'] ?? null;

        // 是否已经评价
        $comment = ConsultingComment::where('tradeId', $tradeId)
            ->find();
        if ($comment) {
            throw new ValidateException('您已经评价了，请勿重复评价');
        }

        $trade = Trade::where('id', $tradeId)->find();
        if (!$trade) {
            throw new ValidateException('找不到该订单');
        }

        $consultingId = $trade->consultingId;

        // 咨询
        $consulting = Consulting::where('id', $consultingId)
            ->find();
        if (!$consulting) {
            throw new ValidateException('找不到该咨询');
        }
        if ($consulting->memberId != $memberId) {
            throw new ValidateException('您不能评价该咨询');
        }

        $commentParams = [
            'consultingId' => $consulting->id,
            'tradeId' => $tradeId,
            'memberId' => $consulting->memberId,
            'lawyerId' => $consulting->lawyerId,
            'level' => $params['level'],
            'comment' => $params['comment'],
        ];

        $this->consultingCommentDao->insertActive($commentParams, []);

        // 同时订单上也保存
        $trade->isComment = 1;
        $trade->commentLevel = $params['level'];
        $trade->comment = $params['comment'];
        $trade->save();

        $job_data = ['id' => $consulting->lawyerId];
        Queue::push(ConsultingCommentStatistics::class, $job_data);
    }

    private function commentMessage($memberId, $params)
    {
        $consultingMessageId = $params['consultingMessageId'] ?? null;

        // 是否已经评价
        $comment = ConsultingComment::where('consultingMessageId', $consultingMessageId)
            ->find();
        if ($comment) {
            throw new ValidateException('您已经评价了，请勿重复评价');
        }

        // 消息
        $message = ConsultingMessage::where('id', $consultingMessageId)
            ->where('messageType', ConsultingMessage::COMMENT)
            ->find();
        if (!$message) {
            throw new ValidateException('找不到该卡片消息');
        }

        $consultingId = $message->consultingId;

        // 咨询
        $consulting = Consulting::where('id', $consultingId)
            ->find();
        if (!$consulting) {
            throw new ValidateException('找不到该咨询');
        }
        if ($consulting->memberId != $memberId) {
            throw new ValidateException('您不能评价该咨询');
        }

        $commentParams = [
            'consultingId' => $consulting->id,
            'consultingMessageId' => $consultingMessageId,
            'memberId' => $consulting->memberId,
            'lawyerId' => $consulting->lawyerId,
            'level' => $params['level'],
            'comment' => $params['comment'],
        ];

        $this->consultingCommentDao->insertActive($commentParams, []);

        // 重新推送评价消息，提醒前端有更新
        $this->getMessageSendService()->retransmission($consultingMessageId);

        $job_data = ['id' => $consulting->lawyerId];
        Queue::push(ConsultingCommentStatistics::class, $job_data);
    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }
}
